/*
 * PointTable.h
 *
 *  Created on: 25/01/2012
 *      Author: paula
 */

#ifndef POINTTABLE_H_
#define POINTTABLE_H_

using namespace std;
#include "Point.h"
#include <tr1/unordered_set>


///
/// Returns an integer associated with a double. Used to build
/// a hash function for points.
///
inline std::size_t hashDouble (double x) {
    union {
        double d;
        size_t s;
    } converter;
    converter.d = x;
    return converter.s;
}

//
// A Hash function for points in 2D
//
struct HashPoint2D : std::unary_function<Point, std::size_t>
{
    std::size_t operator()(const Point& p) const
    {
        return hashDouble (p.coord[0]) ^ hashDouble (p.coord[1]);
    }
};

///
/// A point table is implemented as a set of points.
///
struct PointTable : std::tr1::unordered_set<Point,HashPoint2D> {

	PointTable::iterator insertPoint(double x, double y){
		/*
		 * Insert point in a PointTable
		 *
		 * The function insert determines whether an element X exists in the sequence
		 * whose key has equivalent ordering to that of val. If not, it creates such an element X
		 * and initializes it with val. The function then determines the iterator where that designates X.
		 * If an insertion occurred, the function returns std::pair(where, true).
		 * Otherwise, it returns std::pair(where, false)
		 */

		pair<PointTable::iterator, bool> pa1;
		pa1 = insert(Point(x,y)); //insert in hashtable
		cout << "p1 success == "<< boolalpha << pa1.second << endl;
		return pa1.first;
	}

	PointTable::iterator insertPoint(Point p){
		/*
		 * Insert point in a PointTable
		 *
		 * The function insert determines whether an element X exists in the sequence
		 * whose key has equivalent ordering to that of val. If not, it creates such an element X
		 * and initializes it with val. The function then determines the iterator where that designates X.
		 * If an insertion occurred, the function returns std::pair(where, true).
		 * Otherwise, it returns std::pair(where, false)
		 */

		pair<PointTable::iterator, bool> pa1;
		pa1 = insert(p); //insert in hashtable
		cout << "p1 success == "<< boolalpha << pa1.second << endl;
		return pa1.first;
	}
};



#endif /* POINTTABLE_H_ */
